package com.fr.tree;

import java.util.Arrays;
import java.util.List;
import java.util.Stack;

import org.apache.commons.collections.CollectionUtils;

public class TreeTest {
    private static GroupTree node = null;

    public static void main(String[] args) {
        init();
        filter(101L, node);
        show(node);
    }

    private static void show(GroupTree node2) {
        System.out.println(node);
    }

    private static void filter(long user, GroupTree node) {
        if (node.getLeader() == user) {
            setTrue(node);
            return;
        }
        if (node.getHunterIds().contains(user)) {
            node.setHunterIds(Arrays.asList(user));
        } else {
            node.setHunterIds(null);
        }
        List<GroupTree> sub = node.getSubTree();
        if (CollectionUtils.isNotEmpty(sub)) {
            for (GroupTree g : sub) {
                filter(user, g);
            }
        }
    }

    private static void setTrue(GroupTree node) {
        Stack<GroupTree> stack = new Stack<>();
        stack.push(node);
        GroupTree item = stack.pop();
        while (item != null) {
            item.setCanHit(true);
            stack.addAll(item.getSubTree());
            if (stack.isEmpty()) {
                break;
            }
            item = stack.pop();
        }
    }

    private static void init() {
        node = new GroupTree();
        node.setLeader(1001L);
        node.setGroupId(1);
        node.setHunterIds(Arrays.asList(101L, 102L, 103L));
        GroupTree g1 = new GroupTree();
        g1.setGroupId(2);
        g1.setHunterIds(Arrays.asList(1002L, 1003L, 1004L));
        g1.setLeader(1002L);
        GroupTree g2 = new GroupTree();
        g2.setGroupId(3);
        g2.setHunterIds(Arrays.asList(10002L, 10003L, 10004L));
        g2.setLeader(1002L);
        GroupTree g3 = new GroupTree();
        g3.setGroupId(4);
        g3.setHunterIds(Arrays.asList(100002L, 100003L, 100004L));
        g3.setLeader(1002L);
        node.setSubTree(Arrays.asList(g1, g2, g3));
        GroupTree gg1 = new GroupTree();
        gg1.setGroupId(2);
        gg1.setHunterIds(Arrays.asList(2002L, 2003L, 2004L));
        gg1.setLeader(1002L);
        GroupTree gg2 = new GroupTree();
        gg2.setGroupId(3);
        gg2.setHunterIds(Arrays.asList(20002L, 101L, 20004L));
        gg2.setLeader(1002L);
        GroupTree gg3 = new GroupTree();
        gg3.setGroupId(4);
        gg3.setHunterIds(Arrays.asList(200002L, 200003L, 200004L));
        gg3.setLeader(1002L);
        g1.addSubTree(gg1);
        g1.addSubTree(gg2);
        gg2.addSubTree(gg3);

    }
}
